<?php
// 接口API**********************
//username 用户名
// 接口API**********************
// 获取传入的参数
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$type = $_REQUEST['type'];
 // 从文件中读取数据到PHP变量 
 $json_string =  json_decode(file_get_contents('../json/account.json'),true);
//判断操作类型
if($type=='login'){
    //当操作类型为登录时执行
    //遍历json对象，判断是否登录成功
    $bll = true;
    foreach($json_string as $key => $val){
        //判断登录成功与否
        if($key === $username && $val === $password){
            $bll = false;
            echo '{"err":1,"msg":"登录成功"}';
        }else{
            
        }
      };
      if($bll){
        echo '{"err":-1,"msg":"账号或密码错误"}';
      };


}else if($type === 'noteCode'){
    //当操作类型为获取验证码时执行
    //判断当前手机号是否已注册
    $bll = true;
    foreach($json_string as $key => $val){
        //判断登录成功与否
        if($key === $username){
            $bll = false;
            //创建随机验证码
            $rand1 = rand(100000,999999);
            $json_string[$username] = "$rand1";
            $json_strings = json_encode($json_string);
            file_put_contents("../json/account.json",$json_strings);
            $bll = false;
            echo '{"err":2,"msg":"'.$rand1.'"}';
        }else{
            
        }
      };
      if($bll){
        echo '{"err":-1,"msg":"该手机还未注册"}';
      };
}

?>